package com.xysd.examproject.widget;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.xysd.examproject.R;
import com.xysd.examproject.adapter.WifiListAdapter;
import com.xysd.examproject.util.ToastUtil;
import com.xysd.examproject.util.wifi.WifiAdmin;

import java.util.List;

/**
 * Created by donghui on 2017/6/12.
 */

public class WIFIDialog extends Dialog implements View.OnClickListener {
    private WifiAdmin mWifiAdmin;
    private WifiManager mWifiManager;
    private WifiInfo mWifiInfo;
    private Context context;
    private Button open_wifi,close_wifi,check_wifi,scan_wifi,getWIFIInfo,wifi_hotspot;
    private TextView wifi_info;
    private ListView wifi_list;
    List<ScanResult> mWifiList;


    public WIFIDialog(Context context) {
        super(context);
        this.context=context;
        init();
    }

    public WIFIDialog(Context context, int themeResId) {
        super(context, themeResId);
    }

    protected WIFIDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    private void init() {
        mWifiAdmin=new WifiAdmin(context);
        mWifiManager= (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
        setContentView(R.layout.wifidialog);
        wifi_info= (TextView) findViewById(R.id.wifi_info);
        wifi_list= (ListView) findViewById(R.id.wifi_list);

        wifi_list.setOnItemClickListener(onItemClickListener);

        open_wifi= (Button) findViewById(R.id.open_wifi);
        close_wifi= (Button) findViewById(R.id.close_wifi);
        check_wifi= (Button) findViewById(R.id.check_wifi);
        scan_wifi= (Button) findViewById(R.id.scan_wifi);
        getWIFIInfo= (Button) findViewById(R.id.getWIFIInfo);
        wifi_hotspot= (Button) findViewById(R.id.wifi_hotspot);
        open_wifi.setOnClickListener(this);
        close_wifi.setOnClickListener(this);
        check_wifi.setOnClickListener(this);
        scan_wifi.setOnClickListener(this);
        getWIFIInfo.setOnClickListener(this);
        wifi_hotspot.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.open_wifi:
                mWifiAdmin.openWifi(context);
                break;
            case R.id.close_wifi:
                mWifiAdmin.closeWifi(context);
                break;
            case R.id.check_wifi:
                mWifiAdmin.checkState(context);
                break;
            case R.id.scan_wifi:
                //仅系统应用可调取此权限，故这里注释掉，改用低版本targetSDK解决6.0以上版本无法扫描的问题
//                if (Build.VERSION.SDK_INT >= 23) {
//                    Settings.Secure.putInt(context.getContentResolver(),Settings.Secure.LOCATION_MODE, 1);
//                }
                mWifiAdmin.startScan(context);
                mWifiList=mWifiAdmin.getWifiList();
                if (mWifiList!=null){
                    wifi_list.setAdapter(new WifiListAdapter(context,mWifiList));
//                    new CountListviewHeight().setListViewHeightBasedOnChildren(wifi_list);
                }else {
                    ToastUtil.show("没有可用wifi");
                }
                ToastUtil.show(""+mWifiList.size());
                Log.i("wifilist",mWifiList.size()+"");
                break;
            case R.id.getWIFIInfo:
                setWifiInfo();
                break;
            case R.id.wifi_hotspot:

                break;
            default:
                break;
        }

    }


    String ssid;
    private AdapterView.OnItemClickListener onItemClickListener=new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            dismiss();
            AlertDialog.Builder alert=new AlertDialog.Builder(context);
            ssid=mWifiList.get(position).SSID;
            alert.setTitle(ssid);
            alert.setMessage("输入密码");
            final EditText et_password=new EditText(context);
            final SharedPreferences preferences=context.getSharedPreferences("wifi_password",Context.MODE_PRIVATE);
            et_password.setText(preferences.getString(ssid, ""));
            alert.setView(et_password);
            //alert.setView(view1);
            alert.setPositiveButton("连接", new DialogInterface.OnClickListener(){
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    String pw = et_password.getText().toString();
                    if(null == pw  || pw.length() < 8){
                        Toast.makeText(context, "密码至少8位", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    SharedPreferences.Editor editor=preferences.edit();
                    editor.putString(ssid, pw);
                    editor.commit();
                    mWifiAdmin.addNetwork(mWifiAdmin.CreateWifiInfo(ssid, et_password.getText().toString(), 3));
                }
            });
            alert.setNegativeButton("取消", new DialogInterface.OnClickListener(){
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    //
                    //mWifiAdmin.removeWifi(mWifiAdmin.getNetworkId());
                }
            });
            alert.create();
            alert.show();

        }

    };

    private void setWifiInfo() {
        mWifiInfo=mWifiManager.getConnectionInfo();
        if(null != mWifiInfo && null != mWifiInfo.getSSID()){
            String info = "getSSID()="+mWifiInfo.getSSID()+"\n"
                    +"getBSSID()="+mWifiInfo.getBSSID()+"\n"
                    +"getHiddenSSID()="+mWifiInfo.getHiddenSSID()+"\n"
                    +"getLinkSpeed()="+mWifiInfo.getLinkSpeed()+"\n"
                    +"getMacAddress()="+mWifiInfo.getMacAddress()+"\n"
                    +"getNetworkId()="+mWifiInfo.getNetworkId()+"\n"
                    +"getRssi()="+mWifiInfo.getRssi()+"\n"
                    +"getSupplicantState()="+mWifiInfo.getSupplicantState()+"\n"
                    +"getDetailedStateOf()="+mWifiInfo.getDetailedStateOf(mWifiInfo.getSupplicantState());
            wifi_info.setText(info);
        }else {
            wifi_info.setText("");
        }
    }
}
